/**
 * File: ResultSearchController.java
 * Created by: mhaimel
 * Created on: 1 Oct 2009
 * CVS:  $Id: ResultSearchController.java,v 1.2 2009/11/24 16:28:54 mhaimel Exp $
 */
package uk.ac.ebi.curtain.controller.general;

import java.io.File;

import uk.ac.ebi.curtain.controller.SimpleController;
import uk.ac.ebi.curtain.controller.velvet.VelvetOutputBean;
import uk.ac.ebi.curtain.management.InterceptorInfo;
import uk.ac.ebi.curtain.utils.CurtainUncheckedException;
import uk.ac.ebi.curtain.utils.data.FileType;
import uk.ac.ebi.curtain.utils.data.ReadType;
import uk.ac.ebi.curtain.utils.file.FileInfo;
import uk.ac.ebi.velvet.model.Supercontig;

/**
 * @author mhaimel
 *
 */
public class ResultSearchController extends SimpleController<File, VelvetOutputBean> {

	private static final String CONTIGS_FA = "contigs.fa";

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected VelvetOutputBean processData(File bucketDir, InterceptorInfo info) {
		return _findAndSubmit(bucketDir);
	}

	private VelvetOutputBean _findAndSubmit(File bucket) {
		getLog().debug("Process " + bucket + " ... ");
		VelvetOutputBean bean = new VelvetOutputBean();
		try{
			FileInfo rCtg = Supercontig.getContigDumpFile(bucket);
			String name = Supercontig.getVelvetDirName(getContext().getArguments().getKmer());
			File tCtg = new File(new File(bucket,name),CONTIGS_FA);
			if(tCtg.exists() && rCtg.getFile().exists() 
//					&& tCtg.length() > 0 && rCtg.length() > 0 
					){
				bean.setInputContig(rCtg);
				bean.setOutputContig(new FileInfo(tCtg,FileType.fasta,ReadType.Contig));
			} else {
				bean = null;
			}
			return bean;
		}catch (Exception e) {
			throw new CurtainUncheckedException("Problems building bucket: " + bucket,e);
		}
	}
}
